<?php
class Default_Model_UsersMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Default_Model_DbTable_Users');
		}
		return $this->_dbTable;
	}
	
	public function getDb() {
		if ($this->db === null) {
			$this->db=Zend_Registry::get('connectDB');
		}
		return $this->db;
	}
	
	public function save(Default_Model_TableColumn_Users $user) {
		$date_format = Default_Model_TableColumn_Users::DATE_FORMAT_FULL_EN;
		$now = date($date_format, time());
		$data = array(
			'id'=>$user->getId(),
			'email'=>$user->getEmail(),
			'password'=>$user->getPassword(),
			'username'=>$user->getUsername(),
			'gioi_tinh'=>$user->getGioi_tinh(),
			'ho_ten'=>$user->getHo_ten(),
			'ngay_sinh'=>$user->getNgay_sinh($date_format),
			'dia_chi'=>$user->getDia_chi(),
			'hinh_anh'=>$user->getHinh_anh(),
			'dien_thoai'=>$user->getDien_thoai(),
			'dang_nhap_cuoi'=>$user->getDang_nhap_cuoi(),
			'thanh_pho'=>$user->getThanh_pho(),
			'ngay_tao'=>$user->getNgay_tao(),
			'ngay_cap_nhat'=>$now,
			'ma_kich_hoat'=>$user->getMa_kich_hoat(),
		);
	
		switch ($data['gioi_tinh']) {
			case 0:
				$data['gioi_tinh'] = false;
				break;
			case 1:
				$data['gioi_tinh'] = true;
				break;
		}
		
		if (0 == ($id = $user->getId())) {
			unset($data['id']);
			$data['ngay_tao'] = $now;
			$data['dang_nhap_cuoi'] = $now;
			$data['tai_khoan_chinh'] = 0;
			$data['tai_khoan_khuyen_mai'] = 100000;
			$data['diem'] = 0;
			$data['kich_hoat'] = FALSE;
			
			$this->getDbTable()->insert($data);
		} else {
			unset($data['email']);
			unset($data['username']);
			unset($data['password']);
			unset($data['ma_kich_hoat']);
			unset($data['ngay_kich_hoat']);
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}

	public function fetch($query = null, $option ='view') {
		
		$table = $this->getDbTable();
        $select = $table->select();
        $select->from($table, array('id', 'username', 'email', 'ho_ten', 'gioi_tinh', 'ngay_sinh', 'dia_chi', 'hinh_anh', 'dien_thoai', 'thanh_pho', 'tai_khoan_chinh', 'tai_khoan_khuyen_mai'));
        
        if ($query != null)
        	$select->where($query);
        	
        $resultSet = $this->getDbTable()->fetchAll($select);
        
		return $this->processResultSet($resultSet->toArray(), $option);

	}
	
	/*
	 *processResultSet
	 */
	private function processResultSet($resultSet, $option = 'view') {
		$entries = "";
		if($option == 'view') {
			foreach ($resultSet as $row) {
				$entries = new Default_Model_TableColumn_Users($row);
			}
		} else {
			$entries = array();
			foreach ($resultSet as $row) {
				$entry = new Default_Model_TableColumn_Users($row);
				$entries[] = $entry;
			}
		}
		return $entries;
	}
	/*
	 *fetch
	 */
	public function fetchAll($query = null, $option = 'view') {
		if ($query === NULL) {
            $resultSet = $this->getDbTable()->fetchAll();
        } else {
            $table = $this->getDbTable();
            $select = $table->select();
            $select->from($table)
                   ->where($query);
            $resultSet = $this->getDbTable()->fetchAll($select);
        }
        return $this->processResultSet($resultSet->toArray(),$option);
	}

	public function getLastId() {
		return $this->getDbTable()->getAdapter()->lastInsertId();
	}
	
	public function CheckActivekey($username){
		$select = $this->getDbTable()->select()->where("(username = ? or email = ?) and kich_hoat = 1", $username);
		return $this->getDbTable()->fetchRow($select);
	}
	
	public function activeUserAccount($code){
		$select = $this->getDbTable()->select()->where("ma_kich_hoat = ?", $code);
		$user = $this->getDbTable()->fetchRow($select);
		if($user != null) {
			$data = array('kich_hoat' => true, 'ma_kich_hoat' => true);
			$this->getDbTable()->update($data, 'id = '. $user['id']);
			return true;
		}
		return false;
	}
	
	/*
	 * change item
	 */
	public function change($id, $data = null, $query = null) {
		try {
			if ($query == null) {
				$where = 'id IN (' . $id . ')';
			} else {
				$where = $query;
			}

			$this->getDbTable()->update($data, $where);
		} catch (Exception $e) {
			return false;
		}
		
		return true;
	}

}